#include "canvas_api.h"

void JOUI::Jo_UICanvas_Create(LPVOID handle, INT uWidth, INT uHeight, LPVOID& dst)
{
	auto pObj = (UIWnd*)handle;
	if (pObj->m_UIWindow == nullptr)
		pObj = (UIWnd*)((UIControl*)handle)->m_UIWindow;

	dst = new UICanvas(pObj, uWidth, uHeight);
}

void JOUI::Jo_UICanvas_Destroy(LPVOID hCanvas)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		delete fCanvas;
}

HRESULT JOUI::Jo_UICanvas_OutBloom(LPVOID hCanvas, LPVOID canvas_src, INT scRGB, POINT padding, FLOAT fblur)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->DrawBloom((UICanvas*)canvas_src, scRGB, padding, fblur);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_OutBloomEx(LPVOID hCanvas, LPVOID canvas_src, UIimage hImg, INT shadowColor, BOOL isBloom, FLOAT pMaximumlight, FLOAT pBlur)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->DrawBloom((UICanvas*)canvas_src, (UIImage*)hImg, shadowColor, isBloom, pMaximumlight, pBlur);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_Toimg(LPVOID hCanvas, UIimage** dstImg)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->ToImage((UIImage**)dstImg);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_blur(LPVOID hCanvas, FLOAT fDeviation, RECT* lprc)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->blur(nullptr, fDeviation, lprc);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_CalctextSize(LPVOID hFont, LPCWSTR lpwzText, LONG_PTR dwLen, INT dwDTFormat, FLOAT layoutWidth, FLOAT layoutHeight, FLOAT* lpWidth, FLOAT* lpHeight)
{
	return JOUI::UICanvas::CalcTextSize((UIFont*)hFont, lpwzText, dwLen, dwDTFormat, layoutWidth, layoutHeight, lpWidth, lpHeight);
}

HRESULT JOUI::Jo_UICanvas_Begindraw(LPVOID hCanvas)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->BeginDraw();
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_Enddraw(LPVOID hCanvas)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->EndDraw();
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_Clear(LPVOID hCanvas, INT Color)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->Clear(Color);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_ClipRect(LPVOID hCanvas, FLOAT left, FLOAT top, FLOAT right, FLOAT bottom)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->SetClipRect(left, top, right, bottom);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_ResetClip(LPVOID hCanvas)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->ResetClip();
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_DrawImage(LPVOID hCanvas, UIimage hImage, FLOAT Left, FLOAT Top, DWORD alpha)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->DrawImage((UIImage*)hImage, Left, Top, alpha);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_DrawImageRect(LPVOID hCanvas, UIimage hImage, FLOAT Left, FLOAT Top, FLOAT Right, FLOAT Bottom, INT mode, DWORD alpha, INT radius)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->DrawImageRect((UIImage*)hImage, Left, Top, Right, Bottom, (ImageMode)mode, alpha, radius);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_DrawImageRectRect(LPVOID hCanvas, UIimage hImage, FLOAT dstLeft, FLOAT dstTop, FLOAT dstRight, FLOAT dstBottom,
	FLOAT srcLeft, FLOAT srcTop, FLOAT srcRight, FLOAT srcBottom, INT mode, DWORD alpha, INT radius)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->DrawImagePartRect((UIImage*)hImage, Left, Top, Right, Bottom, srcLeft, srcTop, srcRight, srcBottom,(ImageMode)mode, alpha, radius);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_DrawImageFromGrid(LPVOID hCanvas, UIimage hImage, FLOAT dstLeft, FLOAT dstTop, FLOAT dstRight, FLOAT dstBottom, FLOAT srcLeft, FLOAT srcTop, 
	FLOAT srcRight, FLOAT srcBottom, FLOAT gridPaddingLeft, FLOAT gridPaddingTop, FLOAT gridPaddingRight, FLOAT gridPaddingBottom, INT dwFlags, DWORD dwAlpha)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
	{
		info_GridsImage info{ gridPaddingLeft ,gridPaddingTop ,gridPaddingRight ,gridPaddingBottom ,(uint32_t)dwFlags };

		return fCanvas->DrawGridsImagePart((UIImage*)hImage, Left, Top, Right, Bottom, srcLeft, srcTop, srcRight, srcBottom, &info);
	}
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_DrawLine(LPVOID hCanvas, UIbrush hBrush, FLOAT X1, FLOAT Y1, FLOAT X2, FLOAT Y2, FLOAT strokeWidth, DWORD strokeStyle, BOOL isRadius)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->DrawLine((UIBrush*)hBrush, X1, Y1, X2, Y2, strokeWidth, strokeStyle, isRadius);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_DrawPoint(LPVOID hCanvas, UIbrush hBrush, FLOAT x, FLOAT y, FLOAT strokeWidth, BOOL isRadius)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->DrawPoint((UIBrush*)hBrush, x, y, strokeWidth, isRadius);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_DrawPoly(LPVOID hCanvas, UIbrush hBrush, FLOAT left, FLOAT top, FLOAT right, FLOAT bottom, UINT NumberOfEdges, FLOAT Angle, FLOAT strokeWidth, UINT strokeStyle)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->DrawPoly((UIBrush*)hBrush, left, top, right, bottom, NumberOfEdges, Angle, strokeWidth, strokeStyle);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_FillPoly(LPVOID hCanvas, UIbrush hBrush, FLOAT left, FLOAT top, FLOAT right, FLOAT bottom, UINT NumberOfEdges, FLOAT Angle)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->FillPoly((UIBrush*)hBrush, left, top, right, bottom, NumberOfEdges, Angle);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_DrawPath(LPVOID hCanvas, LPVOID hPath, UIbrush hBrush, FLOAT strokeWidth, DWORD strokeStyle)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->DrawPath((UIBrush*)hBrush, (UIPath*)hPath, strokeWidth, strokeStyle);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_FillPath(LPVOID hCanvas, LPVOID hPath, UIbrush hBrush)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->FillPath((UIBrush*)hBrush, (UIPath*)hPath);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_DrawRect(LPVOID hCanvas, UIbrush hBrush, FLOAT left, FLOAT top, FLOAT right, FLOAT bottom, FLOAT strokeWidth, DWORD strokeStyle)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->DrawRect((UIBrush*)hBrush, left, top, right, bottom, strokeWidth, strokeStyle);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_FillRect(LPVOID hCanvas, UIbrush hBrush, FLOAT left, FLOAT top, FLOAT right, FLOAT bottom)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->FillRect((UIBrush*)hBrush, left, top, right, bottom);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_DrawRoundedrect(LPVOID hCanvas, UIbrush hBrush, FLOAT left, FLOAT top, FLOAT right, FLOAT bottom, FLOAT radius, FLOAT strokeWidth, DWORD strokeStyle)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->DrawRoundRect((UIBrush*)hBrush, left, top, right, bottom, radius, strokeWidth, strokeStyle);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_FillRoundedrect(LPVOID hCanvas, UIbrush hBrush, FLOAT left, FLOAT top, FLOAT right, FLOAT bottom, FLOAT radius)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->FillRoundRect((UIBrush*)hBrush, left, top, right, bottom, radius);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_DrawText(LPVOID hCanvas, LPCWSTR lpwzText, LPVOID hFont, FLOAT left, FLOAT top, FLOAT right, FLOAT bottom, INT crText, INT dwDTFormat)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->DrawTextByColor((UIFont*)hFont, lpwzText, -1, dwDTFormat, left, top, right, bottom, crText);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_DrawelIipse(LPVOID hCanvas, UIbrush hBrush, FLOAT left, FLOAT top, FLOAT right, FLOAT bottom, FLOAT strokeWidth, DWORD strokeStyle)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->DrawEllipse((UIBrush*)hBrush, left, top, right, bottom, strokeWidth, strokeStyle);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_Fillellipse(LPVOID hCanvas, UIbrush hBrush, FLOAT left, FLOAT top, FLOAT right, FLOAT bottom)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->FillEllipse((UIBrush*)hBrush, left, top, right, bottom);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_FillRegion(LPVOID hCanvas, LPVOID hRgn, UIbrush hBrush)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->FillRegion((UIBrush*)hBrush, (UIRegion*)hRgn);
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_Flush(LPVOID hCanvas)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->Flush();
	return S_FALSE;
}

HDC JOUI::Jo_UICanvas_GetDc(LPVOID hCanvas)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
	{
		HDC rdc = nullptr;
		fCanvas->GetDC(&rdc);
		return rdc;
	}
	return 0;
}

HRESULT JOUI::Jo_UICanvas_ReleaseDc(LPVOID hCanvas)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->ReleaseDC();
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_GetSize(LPVOID hCanvas, INT& width, INT& height)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
	{
		SIZE rel{};
		rel = fCanvas->GetSize();
		width = rel.cx;
		height = rel.cy;
		return S_OK;
	}
	return S_FALSE;
}

HRESULT JOUI::Jo_UICanvas_Resize(LPVOID hCanvas, INT width, INT height)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->Resize(width, height);
	return S_FALSE;
}

void JOUI::Jo_UICanvas_Rotate(LPVOID hCanvas, FLOAT fAngle, FLOAT scaleX, FLOAT scaleY)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		fCanvas->Rotate(fAngle, scaleX, scaleY);
}

HRESULT JOUI::Jo_UICanvas_SetTransform(LPVOID hCanvas, LPVOID matrix)
{
	auto fCanvas = (UICanvas*)hCanvas;
	if (fCanvas)
		return fCanvas->SetTransform((ExMatrix*)matrix);
	return S_FALSE;
}

